<?php

/**
 * expense actions.
 *
 * @package    sf_sandbox
 * @subpackage expense
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class expenseActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->startdate=$request->getParameter('startdate');
    if(!$this->startdate)$this->startdate=MyDateTime::today()->getstartofmonth()->todate();
    $this->enddate=$request->getParameter('enddate');
    if(!$this->enddate)$this->enddate=MyDateTime::today()->getendofmonth()->todate();

    $query = Doctrine::getTable('Transaction')
      ->createQuery('a')
      ->where('a.account_id is not null');
    $query->andWhere('a.date >= "'.$this->startdate.'"');
    $query->andWhere('a.date <= "'.$this->enddate.'"');
      
    $this->pager = new sfDoctrinePager('Transaction',100);//items per page
    $this->pager->setQuery($query);
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
    $this->transaction_list = $this->pager->getResults();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->transaction = new Transaction();
    $this->transaction->setDate(MyDateTime::today()->todate());
    $this->form = new ExpenseForm($this->transaction);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new ExpenseForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($transaction = Doctrine::getTable('Transaction')->find(array($request->getParameter('id'))), sprintf('Object transaction does not exist (%s).', array($request->getParameter('id'))));
    $this->form = new ExpenseForm($transaction);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($transaction = Doctrine::getTable('Transaction')->find(array($request->getParameter('id'))), sprintf('Object transaction does not exist (%s).', array($request->getParameter('id'))));
    $this->form = new ExpenseForm($transaction);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($transaction = Doctrine::getTable('Transaction')->find(array($request->getParameter('id'))), sprintf('Object transaction does not exist (%s).', array($request->getParameter('id'))));
    $transaction->delete();

    $this->redirect('expense/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));

    $requestparams=$request->getParameter($form->getName());

    //if income <=0
    if ($requestparams['expense']<=0)
    {
      $this->getUser()->setFlash('error', "Amount must be a positive number");
      return;
    }

    if ($form->isValid())
    {
      $transaction = $form->save();

      $this->redirect('expense/index');
    }
  }
}
